package com.android.bookgarden.recycleview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.bookgarden.utli.Utlis;


/**
 * Created by zouqh on 2017/3/2.
 * 自定义分割线
 * http://www.jianshu.com/p/b46a4ff7c10a
 */

public class GridViewItemDecoration extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private int dividerHeight;
//    private Paint mPaint = new Paint();
private LinearGradient mLinearGradient =
        new LinearGradient(0, 0, 0, 100, new int[] { Color.BLACK, 0 }, null,
                Shader.TileMode.CLAMP);
    private Drawable mDivider;
    public GridViewItemDecoration(Context context) {
        dividerHeight = Utlis.px2dip(context, 2);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor("#F2F2F2"));
    }
    public GridViewItemDecoration(Context context, float higth, String cs) {
        dividerHeight = Utlis.px2dip(context, higth);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor(cs));
//        mDivider = new ColorDrawable(Color.parseColor(cs));
    }

//    /**
//     * 可以实现类似绘制背景的效果，内容在上面
//     */
//    @Override
//    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//        super.onDraw(c, parent, state);
//
//        int childCount = parent.getChildCount();
//        int left = parent.getPaddingLeft();
//        int right = parent.getWidth() - parent.getPaddingRight();
//
//        for (int i = 0; i < childCount - 1; i++) {
//            View view = parent.getChildAt(i);
//            float top = view.getBottom();
//            float bottom = view.getBottom() + dividerHeight;
//            c.drawRect(left, top, right, bottom, mPaint);
//        }
//    }
//
//    /**
//     * 以绘制在内容的上面，覆盖内容
//     */
//    @Override
//    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
//        super.onDrawOver(c, parent, state);
//    }
//
//    /**
//     * 可以实现类似padding的效果
//     */
//    @Override
//    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//        super.getItemOffsets(outRect, view, parent, state);
//        outRect.left = dividerHeight;//留出画线的距离
//    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //获得RecyclerView中总条目数量
        int childCount = parent.getChildCount();

        //遍历一下
        for (int i = 0; i < childCount; i++) {
            if (i == 0) {
                //如果是第一个条目，那么我们就不画边框了
                continue;
            }
            //获得子View，也就是一个条目的View，准备给他画上边框
            View childView = parent.getChildAt(i);

            //先获得子View的长宽，以及在屏幕上的位置，方便我们得到边框的具体坐标
            float x = childView.getX();
            float y = childView.getY();
            int width = childView.getWidth();
            int height = childView.getHeight();

            //根据这些点画条目的四周的线
            c.drawLine(x, y, x + width, y, mPaint);
            c.drawLine(x, y, x, y + height, mPaint);
            c.drawLine(x + width, y, x + width, y + height, mPaint);
            c.drawLine(x, y + height, x + width, y + height, mPaint);

            //当然了，这里大家肯定是要根据自己不同的设计稿进行画线，或者画一些其他的东西，都可以在这里搞，非常方便
        }
        super.onDraw(c, parent, state);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        mPaint.setShader(mLinearGradient);
        c.drawRect(dividerHeight, 0, parent.getRight(), 0, mPaint);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        int itemPosition =
                ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
        int totalItemCount = parent.getAdapter().getItemCount();
        if (itemPosition == totalItemCount - 1) {
            outRect.set(0, 0, 0, 0);
        } else {
            outRect.set(dividerHeight, 0, 0, 0);
        }
    }
}
